/*
 * ????
*/
var Util = new Object();
Util.getUserAgent = navigator.userAgent;
Util.isGecko = Util.getUserAgent.indexOf("Gecko") != -1;
Util.isOpera = Util.getUserAgent.indexOf("Opera") != -1;


Array.prototype.contains = function(item){
    var arry = this;
	for(var i=0;i<arry.length;i++){
	   
	   if(arry[i] = item){
	   
	      return true;
	   
	   }else{
	   
	     return false;
	   }
	
	
	}
  

}

Util.getOffset = function (el) {
   /**
	var retValue = 0;

    var scrollValue=0;

    while (el!=null) {
              
	    retValue += el["offset" + (isLeft ? "Left" : "Top")] || 0;
       
		el =Util.offsetParent(el);

		if(el.tagName=='BODY') break;
		
		var p = el.style.position;
        if (p == 'relative' || p == 'absolute') break;

		if(el.tagName.toLowerCase()=="div"&& el.style.overflow=="auto")
        {   
		  scrollValue = el["scroll"+ (isLeft ? "Left" : "Top")];
		  retValue = retValue - scrollValue;
	 
        }
		

    }
  

    return retValue;
*/

 var valueT = 0, valueL = 0;
    do {
      valueT += el.offsetTop  || 0;
      valueL += el.offsetLeft || 0;
      el = el.offsetParent;
      if (el) {
        if(el.tagName=='BODY') {
            valueL =  valueL - document.body.scrollLeft;
            valueT =  valueT - document.body.scrollTop;
           break;
        }
        var p = el.style.position;
        if (p == 'relative' || p == 'absolute') break;
		var scrollable = [el.style.overflow,el.style.overflowX,el.style.overflowY];
		
		var isauto = scrollable.contains("auto");

		var isscroll = scrollable.contains("scroll");

		if(el.tagName=="DIV" && (isauto || isscroll)){

		   valueT =  valueT - el.scrollTop;
		   valueL = valueL - el.scrollLeft;
		
		}
      }
    } while (el);
    return [valueL, valueT];



};






Util.bindFunction = function (el, fucName) {
    return function () {
        return el[fucName].apply(el, arguments);
    };
};
Util.re_calcOff = function (el) {
    for (var i = 0; i < Util.dragArray.length; i++) {
        var ele = Util.dragArray[i];
		var position = Util.getOffset(ele.elm);
		ele.elm.pagePosLeft = position[0];
		ele.elm.pagePosTop = position[1];
	
    }
    var nextSib = el.elm.nextSibling;
    while (nextSib) {
        nextSib.pagePosTop -= el.elm.offsetHeight;
        nextSib = nextSib.nextSibling;
    }
};

Util.hide = function () {
    Util.rootElement.style.display = "none";
};

Util.show = function () {
    Util.rootElement.style.display = "";
};

Util.getElementsByClassName = function(node, className) {
	var children = node.getElementsByTagName('*');
	var elements = new Array();
	for (var i=0; i<children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}
	return elements;
}

/*
 * ???????
*/
ghostElement = null;
getGhostElement = function () {

    if (!ghostElement) {  
        ghostElement = document.createElement("DIV");
        ghostElement.className = "divdrag_ghost";
        ghostElement.backgroundColor = "";
        ghostElement.style.border = "3px dashed red";
        ghostElement.innerHTML = "&nbsp;";
    }
    return ghostElement;
};

/*
 * ?????
*/
function draggable(el,header) {
	
    this._dragStart = start_Drag;
    this._drag = when_Drag;
    this._dragEnd = end_Drag;
    this._afterDrag = after_Drag;
    this.isDragging = false;
    this.elm = el;
    this.header = document.getElementById(el.id + header);
    this.hasIFrame = this.elm.getElementsByTagName("IFRAME").length > 0;
    if (this.header) {
        this.header.style.cursor = "move";
        Drag.init(this.header, this.elm);
        this.elm.onDragStart = Util.bindFunction(this, "_dragStart");
        this.elm.onDrag = Util.bindFunction(this, "_drag");
        this.elm.onDragEnd = Util.bindFunction(this, "_dragEnd");
    }
};

var oldEleRoot = null;
function start_Drag() {
	//alert("1start_Drag");
	oldEleRoot = this.elm.parentNode;
    Util.re_calcOff(this);
    this.origNextSibling = this.elm.nextSibling;
    var _ghostElement = getGhostElement();
    _ghostElement.className = this.elm.className;
    
    var offH = this.elm.offsetHeight;
    if (Util.isGecko) {
        offH -= parseInt(_ghostElement.style.borderTopWidth) * 2;
    }
    var offW = this.elm.offsetWidth;
	var position = Util.getOffset(this.elm);
	var offLeft = position[0];
	var offTop = position[1];

    Util.hide();
    this.elm.style.width = offW + "px";
    _ghostElement.style.height = offH + "px";
   
    this.elm.parentNode.insertBefore(_ghostElement, this.elm.nextSibling);
    this.elm.style.position = "absolute";
    this.elm.style.zIndex = 100;
    this.elm.style.left = offLeft + "px";
    this.elm.style.top = offTop + "px";
    Util.show();
    this.isDragging = false;
    return false;
};

function when_Drag(clientX, clientY) {
	//alert("2when_Drag");
    if (!this.isDragging) {
        this.elm.style.filter = "alpha(opacity=70)";
        this.elm.style.opacity = 0.7;
        this.isDragging = true;
    }

    var found = null;
    var max_distance = 100000000;
    for (var i = 0; i < Util.dragArray.length; i++) {
        var ele = Util.dragArray[i];
        var distance = Math.sqrt(Math.pow(clientX - ele.elm.pagePosLeft, 2) + Math.pow(clientY - ele.elm.pagePosTop, 2));
        if (ele == this) {
            continue;
        }
        if (isNaN(distance)) {
            continue;
        }
        if (distance < max_distance) {
            max_distance = distance;
            found = ele;
        }
    }
    var _ghostElement = getGhostElement();
    if (found != null && _ghostElement.nextSibling != found.elm) {
        found.elm.parentNode.insertBefore(_ghostElement, found.elm);
        if (Util.isOpera) {
            document.body.style.display = "none";
            document.body.style.display = "";
        }
    }
   
};



function end_Drag() {
	//alert("3end_Drag");
    if (this._afterDrag()) {
    	 saveNodePosition(this.elm)
    }
    return true;
};





	function saveNodePosition(obj){		
	
		var currentNode = obj;
		
		var parentNode = obj.parentNode;	
		
		for(i = 0; i < parentNode.childNodes.length; i++ ){			
			var node = parentNode.childNodes(i);
			
			if(node.id == currentNode.id){
			 // alert(oldEleRoot.id+"="+node.parentNode.id);
			   if(oldEleRoot.id == node.parentNode.id && oldEleRoot.id=="cut_body0")
			     return ;
				var applyId = node.parentNode.id.substring(node.parentNode.id.length-1);
				
				var ids = node.id.split("_");
				var cutId = ids[1];
				var url = "shortcut.do?method=updateCutMenuWhenMove&applyId="+applyId+"&cutId="+cutId+"&sortId="+i;
				//saveDragSource(url,"menu");
				startRequest("POST",url,null,showParentMenuDiv);//ajaxtool.js里的方法
				
			}
		}
	
	}


//dojo实现ajax方法
 function saveDragSource(url,changeType){

   dojo.io.bind({
				url:url,
				handle: function(type, data, evt){ 
    			    if(type == "load"){
            			if(changeType=="menu"){
    			        	var toolbar = createToolBar("shortcut.do?method=generateToolBarXml");
            				window.parent.document.getElementById("shortcutMenuDiv").innerHTML= toolbar;
    			        }  
    			        
		        	 }else if(type == "error"){ 
						postMsg(data,"error")
			         }else{ 
						postMsg(data,"error")        	
			   	     }
	    		 },
				mimetype:"text/html",
				preventCache: false,
				useCache: false
			})	
			
} 

 

function after_Drag() {
	//alert("after_Drag");	
    var returnValue = false;
    Util.hide();
    this.elm.style.position = "";
    this.elm.style.width = "";
    this.elm.style.zIndex = "";
    this.elm.style.filter = "";
    this.elm.style.opacity = "";
    var ele = getGhostElement();
    if (ele.nextSibling != this.origNextSibling) {
        ele.parentNode.insertBefore(this.elm, ele.nextSibling);
        returnValue = true;
    }
    ele.parentNode.removeChild(ele);
    Util.show();
    if (Util.isOpera) {
        document.body.style.display = "none";
        document.body.style.display = "";
    }   
    return returnValue;
};

/*
 * ??????
*/
var Drag = {
	obj:null, 
	init:function (elementHeader, element) {
    	elementHeader.onmousedown = Drag.start;
	    elementHeader.obj = element;
	    if (isNaN(parseInt(element.style.left))) {
	        element.style.left = "0px";
	    }
	    if (isNaN(parseInt(element.style.top))) {
	        element.style.top = "0px";
	    }
	    element.onDragStart = new Function();
	    element.onDragEnd = new Function();
	    element.onDrag = new Function();
	},
	start:function (event) {
	    var element = Drag.obj = this.obj;
	    event = Drag.fixE(event);
	    if (event.which != 1) {
	        return true;
    	}
	    element.onDragStart();
	    element.lastMouseX = event.clientX;
	    element.lastMouseY = event.clientY;
	    document.onmouseup = Drag.end;
	    document.onmousemove = Drag.drag;
	    return false;
	}, 

	drag:function (event) {
	    event = Drag.fixE(event);
	    if (event.which == 0) {
	        return Drag.end();
    	}
	    var element = Drag.obj;
	    var _clientX = event.clientY;
	    var _clientY = event.clientX;
	    if (element.lastMouseX == _clientY && element.lastMouseY == _clientX) {
	        return false;
	    }
	    var _lastX = parseInt(element.style.top);
	    var _lastY = parseInt(element.style.left);
	    var newX, newY;
	    newX = _lastY + _clientY - element.lastMouseX;
	    newY = _lastX + _clientX - element.lastMouseY;
	    element.style.left = newX + "px";
	    element.style.top = newY + "px";
	    element.lastMouseX = _clientY;
	    element.lastMouseY = _clientX;
	    element.onDrag(newX, newY);
	    return false;
	},
	end:function (event) {
	    event = Drag.fixE(event);
	    document.onmousemove = null;
	    document.onmouseup = null;
	    var _onDragEndFuc = Drag.obj.onDragEnd();
	    Drag.obj = null;
	    return _onDragEndFuc;
	}, 
	fixE:function (ig_) {
	    if (typeof ig_ == "undefined") {
	        ig_ = window.event;
	    }
	    if (typeof ig_.layerX == "undefined") {
	        ig_.layerX = ig_.offsetX;
	    }
	    if (typeof ig_.layerY == "undefined") {
	        ig_.layerY = ig_.offsetY;
	    }
	    if (typeof ig_.which == "undefined") {
	        ig_.which = ig_.button;
	    }
    	return ig_;
	}
};

/*
 * ?????
*/
var _Rss_initDrag = function (el) {	
	alert("_Rss_initDrag");
    Util.rootElement = el;
    Util.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];		
        for (var j = 0; j < ele.childNodes.length; j++) {
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className=="divdrag_body") {            	
                Util.dragArray[counter] = new draggable(ele1,"_h");
                counter++;
            }
        }
    }
};

var _initDrag = function (el,classname) {
	alert("_Rss_initDrag");
    Util.rootElement = el;
    Util.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];		
        for (var j = 0; j < ele.childNodes.length; j++) {
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className.indexOf(classname)>-1) {				
                Util.dragArray[counter] = new draggable(ele1,"_header");
                counter++;
            }
        }
    }
};

var _initDragObj = function (el,classname,header) {		  
    Util.rootElement = el;
	
    Util.dragArray = new Array();
    var counter = 0;
    for (var i = 0; i < el.childNodes.length; i++) {
        var ele = el.childNodes[i];	
		
        for (var j = 0; j < ele.childNodes.length; j++) {        	
            var ele1 = ele.childNodes[j];		
            if (ele1.tagName == "DIV" && ele1.className.indexOf(classname)>-1) {				
                Util.dragArray[counter] = new draggable(ele1,header);
                counter++;
            }
        }
    }
};






//初始化带滚动条的div里的对象的拖拉
var _initScrollDrag = function (el,classname,header) {		  
    Util.rootElement = el[0];	
    Util.dragArray = new Array();
    var counter = 0;
    for(var x=0;x<el.length;x++){
		
		var tempEl = el[x];
			for (var j = 0; j < tempEl.childNodes.length; j++) {        	
				var ele1 = tempEl.childNodes[j];		
				if (ele1.tagName == "DIV" && ele1.className.indexOf(classname)>-1) {				
					Util.dragArray[counter] = new draggable(ele1,header);
					counter++;
				}
			}		
	}
};

